package controller.dictionary;

import controller.common.BaseController;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.FundSetServices;
import validator.NotBlankFluentValidator;
import validator.SizeFluentValidator;
import wt.entity.dictionary.FundSet;

import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("dictionary/fundSet")
public class FundSetController extends BaseController {
    @Autowired
    FundSetServices fundSetServices;


    @RequiresPermissions("dictionary:fundSet:default")
    @RequestMapping("index")
    public String index(){
        return  "dictionary/fundSet/index";
    }


    @RequiresPermissions("dictionary:fundSet:default")
    @RequestMapping("editDlg")
    public String editDlg(@RequestParam(value = "id", required = false) String id, Map map) throws Exception {
        if (id != null) {
            FundSet fundSet ;
            fundSet = fundSetServices.selectByPrimaryKey(id);
            if (fundSet == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG ;
            }
            map.put(StaticConstant.EDIT_DATA, fundSet);
            map.put(StaticConstant.EDIT_URL,"dictionary/fundSet/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE,StaticConstant.EDIT_UPDATE);
        }else{
            map.put(StaticConstant.EDIT_URL,"dictionary/fundSet/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE,StaticConstant.EDIT_INSERT);
        }
        return "dictionary/fundSet/editDlg";
    }
    @RequiresPermissions("dictionary:fundSet:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult add(FundSet fundSet)  throws Exception{
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(fundSet.getName(), new NotBlankFluentValidator("名称"))
                .on(fundSet.getDayMoney(),new SizeFluentValidator(0,9999,"日薪资"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
             commonResult.addMessage(result);
            return commonResult.setError();
        }

        fundSetServices.insertSelective(fundSet);
        return commonResult.setSuccess();
    }

    @RequiresPermissions("dictionary:fundSet:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(FundSet fundSet)  throws Exception{
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(fundSet.getName(), new NotBlankFluentValidator("名称"))
                .on(fundSet.getDayMoney(),new SizeFluentValidator(0,9999,"日薪资"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        fundSetServices.updateByPrimaryKeySelective(fundSet);
        return commonResult.setSuccess();
    }

    @RequiresPermissions("dictionary:fundSet:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id)  throws Exception{
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }
        fundSetServices.deleteByPrimaryKey(id);
        return commonResult.setSuccess();
    }
    @RequiresPermissions("dictionary:fundSet:default")
    @ResponseBody
    @RequestMapping("select")
    public List<FundSet> select()  throws Exception{
        List<FundSet> fundSetList = null;
        fundSetList = fundSetServices.selectByExample(null);
        return fundSetList;
    }

}
